package com.LeeCode;

/**
 * 最小化数组中的最大值
 */

public class Code2439 {
    public static void main(String[] args) {
        int[] nums = {3, 7, 1, 6};

        System.out.println(new Code2439().minimizeArrayValue(nums));
    }

    public int minimizeArrayValue(int[] nums) {
        int left = 0, right = 0;
        for (int num : nums) {
            right = Math.max(right, num);
        }

        while (left + 1 < right) {
            int mid = left + (right - left) / 2;
            if (check(nums, mid))
                right = mid;
            else
                left = mid;
        }
        return right;
    }


    private boolean check(int[] nums, int limit) {
        long space = 0;
        for (int num : nums) {
            space += limit - num;
            if (space < 0)
                return false;
        }
        return true;
    }
}
